using System;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.MultiTenancy;

namespace AuthService.Domain.OAuth;

/// <summary>
/// OAuth客户端实体
/// </summary>
public class OAuthClient : FullAuditedAggregateRoot<Guid>, IMultiTenant
{
    /// <summary>
    /// 租户ID
    /// </summary>
    public virtual Guid? TenantId { get; set; }

    /// <summary>
    /// 客户端ID
    /// </summary>
    public virtual string ClientId { get; set; } = string.Empty;

    /// <summary>
    /// 客户端名称
    /// </summary>
    public virtual string ClientName { get; set; } = string.Empty;

    /// <summary>
    /// 客户端密钥
    /// </summary>
    public virtual string ClientSecret { get; set; } = string.Empty;

    /// <summary>
    /// 客户端URI
    /// </summary>
    public virtual string ClientUri { get; set; } = string.Empty;

    /// <summary>
    /// Logo URI
    /// </summary>
    public virtual string LogoUri { get; set; } = string.Empty;

    /// <summary>
    /// 描述
    /// </summary>
    public virtual string Description { get; set; } = string.Empty;

    /// <summary>
    /// 重定向URI列表
    /// </summary>
    public virtual string RedirectUris { get; set; } = string.Empty;

    /// <summary>
    /// 授权类型
    /// </summary>
    public virtual string GrantTypes { get; set; } = string.Empty;

    /// <summary>
    /// 作用域
    /// </summary>
    public virtual string Scopes { get; set; } = string.Empty;

    /// <summary>
    /// 是否启用
    /// </summary>
    public virtual bool IsActive { get; set; } = true;

    /// <summary>
    /// 是否需要客户端密钥
    /// </summary>
    public virtual bool RequireClientSecret { get; set; } = true;

    /// <summary>
    /// 是否允许直接访问授权
    /// </summary>
    public virtual bool AllowDirectAccessGrants { get; set; } = true;

    /// <summary>
    /// 是否允许隐式流程
    /// </summary>
    public virtual bool AllowImplicitFlow { get; set; } = false;

    /// <summary>
    /// 是否允许授权码流程
    /// </summary>
    public virtual bool AllowAuthorizationCodeFlow { get; set; } = true;

    /// <summary>
    /// 访问令牌生存期（秒）
    /// </summary>
    public virtual int AccessTokenLifetime { get; set; } = 3600;

    /// <summary>
    /// 刷新令牌生存期（秒）
    /// </summary>
    public virtual int RefreshTokenLifetime { get; set; } = 2592000;

    /// <summary>
    /// 是否允许离线访问
    /// </summary>
    public virtual bool AllowOfflineAccess { get; set; } = false;

    /// <summary>
    /// 允许的授权类型（别名属性，用于兼容）
    /// </summary>
    public virtual string AllowedGrantTypes => GrantTypes;

    /// <summary>
    /// 允许的作用域（别名属性，用于兼容）
    /// </summary>
    public virtual string AllowedScopes => Scopes;

    protected OAuthClient()
    {
    }

    public OAuthClient(Guid id, string clientId, string clientName) : base(id)
    {
        ClientId = clientId;
        ClientName = clientName;
    }

    /// <summary>
    /// 激活客户端
    /// </summary>
    public virtual void Activate()
    {
        IsActive = true;
    }

    /// <summary>
    /// 停用客户端
    /// </summary>
    public virtual void Deactivate()
    {
        IsActive = false;
    }
}